
// 封装ajax请求
function my_ajax(options) {
    $.ajax({
        type: options.type == null ? "POST" : options.type,
        dataType: "json",
        data: JSON.stringify(options.data),
        contentType: options.contentType == null ? "application/json;charset=utf-8" : options.contentType,
        url: "http://120.55.165.31:8080/Elearning/api" + options.url,
        success: options.success,
        error: options.error == null ? function () {
            alert("发生错误!");
        } : options.error,
        async: options.async == null ? false : options.async
    });
}

// 同步获取数据--全部
function getData(apiUrl, searchKey) {
    let data = null;
    my_ajax({
        url: apiUrl,
        data: {page: 1, number: 10000, courseName:searchKey, teacherName:searchKey},
        success: function (response) {
            data = response.data;
        }
    });
    return data;
}


// 默认按照热度降序
let data = getData( "/all/course/selectCourseByTeacherNameOrCourseNameVague",sessionStorage.getItem("search-key"));

// 初始化
function prepare(){
    setCurPage(1);
    $("#first").addClass("disabled");
    $("#previous").addClass("disabled");
}


$(document).ready(
    function () {
        prepare();
        $(".page").on("click", "a",
            function () {
                let end = getTotalPage();
                let prePageSelector = $(".page");
                let prePage = parseInt(prePageSelector.children(".active").text());
                prePageSelector.children(".active").removeClass("active");
                if ($(this).text() === "上一页") {
                    prePage--;
                } else if ($(this).text() === "首页") {
                    prePage = 1;
                } else if ($(this).text() === "下一页") {
                    prePage++;
                } else if ($(this).text() === "尾页") {
                    prePage = end;
                } else {
                    prePage = parseInt($(this).text());
                }
                setCurPage(prePage);
                if (prePage === 1) {
                    $("#previous").addClass("disabled");
                    $("#first").addClass("disabled");
                }
                if (prePage === end) {
                    $("#next").addClass("disabled");
                    $("#last").addClass("disabled");
                }
            });
    });

// 获取总页数
function getTotalPage() {
    // 没有课程隐藏页码
    let end = Math.ceil(data.total / 10);
    if(end === 0 || end === 1)
    {
        $(".page").css("display", "none");
    }else
        $(".page").css("display", "block");
    return end;
}

//设置当前页码为num并显示当前页码的数据
function setCurPage(num) {
    let end = getTotalPage();
    if(end === 0)
        return;
    let numbers = getPageList(num);
    let head = "<a href=\"#\" id='first'>首页</a>\n" +
        "    <a href=\"#\" id=\"previous\">上一页</a>";
    let tail = "<a href=\"#\" id=\"next\">下一页</a>\n" +
        "    <a href=\"#\" id='last'>尾页</a>";
    let page = "";
    for (let n of numbers) {
        page += "<a href=\"#\">" + n + "</a>";
    }
    let pageSelector = $(".page");
    pageSelector.html(head + page + tail);

    if (num <= 3)
        pageSelector.children("a").eq(num + 1).addClass("active");
    else if (num > 3 && num <= end - 2)
        pageSelector.children("a").eq(4).addClass("active");
    else if (num > end - 2 && end >= 5)
        pageSelector.children("a").eq(6 + num - end).addClass("active");
    else
        pageSelector.children("a").eq(5).addClass("active");

    //获取当前页表的课程并显示
    $("#course-content").empty();
    $("#course-content").html(getCurPageCourse(num));
}

//获取当前要显示的页码形式
function getPageList(num) {
    let end = getTotalPage();
    let numbers = [];
    if (end <= 5) {
        for (let i = 1; i <= end; i++) {
            numbers.push(i);
        }
        return numbers;
    }
    if (num <= 3) {
        return [1, 2, 3, 4, 5];
    }
    numbers = [num - 2, num - 1, num, num + 1, num + 2];
    numbers = numbers.filter((item) => {
        return item > 0 && item <= end;
    });
    while (numbers.length < 5) {
        numbers.unshift(numbers[0] - 1);
    }
    return numbers;
}

// 获取当前要显示的课程
function getCurPageCourse(num) {
    let courseHtml = "";
    for (let i = 0; i < 10 && ((num - 1) * 10 + i) < data.total; i++) {
        let curCourse = data.courses[(num - 1) * 10 + i];
        courseHtml += '<div class="col-lg-2 course-style">\n' +
            '            <a target="_blank" href="\CourseInfo.html?courseId='+ curCourse.courseId + '">\n' +
            '                <img class="course-banner" src="http://120.55.165.31:8080/Elearning/' + curCourse.courseImage + '">\n' +
            '                <div class="course-card">\n' +
            '                    <h5>' + curCourse.courseName + '</h5>\n' +
            '                    <span><i class="fa fa-user" aria-hidden="true">' + curCourse.learnTotal + '</i></span>\n' +
            '                    <span style="margin-left: 120px"><i class="fa fa-jpy" aria-hidden="true">' + curCourse.cost + '</i></span>\n' +
            '                </div>\n' +
            '            </a>\n' +
            '            <p>' + curCourse.courseIntroduction + '</p>\n' +
            '        </div>';
    }
    return courseHtml;
}